<?php

/**
 *
 * @author MerDars
 */
class db extends system{

    private $db = null;

    function __construct(){
        parent::__construct();
        $this->db = new mysqli(xSystemConfig::$db['host'], xSystemConfig::$db['username'], xSystemConfig::$db['password'], xSystemConfig::$db['db']['default']);
        if($this->db->connect_error){
            die('Error: ' . $this->db->connect_errno . ', ' . $this->db->connect_error);
        }
        $this->db->set_charset('utf8');
        return $this;
    }

    function __destruct(){
        return helper::logNum(__CLASS__) ? file_put_contents(xSystemConfig::$originalScriptDir . "/_logs/db-log-" . date("YmdHis") . " [" . helper::clientip() . "].log", helper::log_dump(__CLASS__), FILE_APPEND) : false;
    }

    function query($sql, $resultmode = MYSQLI_STORE_RESULT){
        $time = helper::microtime(true);
        $result = new db_result($this->db->query($sql, $resultmode)) or die("Error: {$this->db->error}");
        helper::LOG(__CLASS__, $sql, $time);
        return $result;
    }

    function affectedRows(){
        return $this->db->affected_rows;
    }

    function insertID(){
        return $this->db->insert_id;
    }

    function esc($escapestr){
        return $this->db->real_escape_string($escapestr);
    }

    //--------------------------------------------------------------------------

    function insert($table, array $data, array $onduplicateupdate = NULL){
        $table = is_array($table) ? "`" . $this->db->esc(array_keys($table)[0]) . "`.`" . $this->db->esc(array_values($table)[0]) . "`" : "`" . $this->db->esc($table) . "`";
        $data = array_map(function($esc){
                    return $this->esc($esc);
                }, $data);
        ;
        $this->db->query("insert into {$table} (`" . implode("`,`", array_keys($data)) . "`) values ('" . implode("','", array_values($data)) . "')" . (
                (($onduplicateupdate = $onduplicateupdate !== NULL ? array_map(function($esc){
                            return $this->esc($esc);
                        }, $onduplicateupdate) : NULL) === NULL) ? ("") : ("on duplicate key update `" . array_keys($onduplicateupdate)[0] . "`='" . array_values($onduplicateupdate)[0] . "'")
                ) . "; ");
        return $this->db->insert_id();
    }
    
    //--------------------------------------------------------------------------
    
    function recheckData($table){
                $table = is_array($table) ? "`" . $this->db->esc(array_keys($table)[0]) . "`.`" . $this->db->esc(array_values($table)[0]) . "`" : "`" . $this->db->esc($table) . "`";

    }
     

}

class db_result extends system{

    private $result = null;

    function __construct($result){
        $this->result = $result;
    }

    function fetch(){
        return $this->result->fetch_assoc();
    }

    function fetchAll(){
        $rows = [];
        while($row = $this->fetch()){
            $rows[] = $row;
        }
        return $rows;
    }

    function free(){
        $this->result->close();
    }

}

?>
